import 'package:dio/dio.dart';
import 'api_end_point.dart';

class ApiService {
  static Dio _dio;
  static ApiService _apiService;

  Dio get dio => _dio;

  ApiService._inner();

  static ApiService getInstance() {
    if (_apiService == null) {
      _apiService = ApiService._inner();
      initDio();
    }
    return _apiService;
  }

  static void initDio() {
    BaseOptions baseOptions = BaseOptions(
      baseUrl: ApiEndPoint.base_url,
      connectTimeout: 60000,
      receiveTimeout: 60000,
      sendTimeout: 30000,
      headers: ApiEndPoint.httpHeader,
    );
    _dio = Dio(baseOptions);
    print('dio被初始化了');
  }

  void get(String api, Function success, Function fail, Function complete,
      [Map<String, dynamic> param]) async {
    await _dio
        .get(api, queryParameters: param)
        .then((value) => success(value.data))
        .catchError((onError) => fail)
        .whenComplete(() => complete);
  }
}
